const path = require('path')
const os = require('os')
const fs = require('fs')

exports.upload = async (files) => {
  const tmpdir = './public/uploads/';
  const filePaths = [];

  for (let key in files) {
    let file = files[key];
    let fileName = new Date().getTime() + '.' + file.type.split('/')[1];
    let filePath = path.join(tmpdir, fileName);
    let reader = fs.createReadStream(file.path);
    let writer = fs.createWriteStream(filePath);
    reader.pipe(writer);
    filePaths.push({path: fileName, size: file.size / 1024 >= 1024 ? (file.size / 1024 / 1024).toFixed(2) + 'MB': (file.size / 1024).toFixed(2) + 'KB', type: file.type});
  }
  return filePaths[0]
}

exports.uploads = async (files) => {
  const tmpdir = '../public/uploads/';
  const filePaths = [];

  for (let key in files) {
    let file = files[key];
    let filePath = path.join(tmpdir, file.name);
    let reader = fs.createReadStream(file.path);
    let writer = fs.createWriteStream(filePath);
    reader.pipe(writer);
    filePaths.push(filePath);
  }
  return filePaths
}